package com.dongdong.service.Impl;


import cn.hutool.core.util.StrUtil;
import com.dongdong.mapper.TbSellerMapper;

import com.dongdong.model.TbSeller;
import com.dongdong.model.TbSellerExample;

import com.dongdong.result.PageBean;
import com.dongdong.result.PageResult;
import com.dongdong.result.Result;
import com.dongdong.result.ResultEnums;
import com.dongdong.service.SellerService;
import com.dongdong.vo.TbSellerVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SellerServiceImpl implements SellerService {

    @Autowired
    private TbSellerMapper tbSellerMapper;


    @Override
    public TbSeller selectSellerByUsername(String name) {
        TbSellerExample example = new TbSellerExample();
        example.createCriteria().andNameEqualTo(name);
        List<TbSeller> tbSellers=tbSellerMapper.selectByExample(example);

        if(tbSellers!=null&& tbSellers.size()>0){
            return tbSellers.get(0);
        }
        return null;
    }



    //保证账号唯一性
    @Override
    public int unameseller(String uname) {
        TbSellerExample ue = new TbSellerExample();
         ue.createCriteria().andNameEqualTo(uname);
        List< TbSeller> list = tbSellerMapper.selectByExample(ue);
        if (list == null|| list.size()==0) {
            return 2;

        }
        return 1;
    }

    @Override
    public Result sellerregiest(TbSellerVO sellerVO) {

        TbSellerExample ue = new TbSellerExample();
        ue.createCriteria().andSellerIdEqualTo(sellerVO.getSellerId());
        List<TbSeller> list = tbSellerMapper.selectByExample(ue);
        if ( list.size()!=0) {

            return Result.ERROR().setData("用户ID不可重名"); //用户ID不可重名

        }

        TbSeller tbSeller = new TbSeller();
        BeanUtils.copyProperties(sellerVO,tbSeller);

        int i = tbSellerMapper.insertSelective(tbSeller);
        if (i == 0) {
            return Result.ERROR();
        }
        return Result.OK();
    }

    //分页
    @Override
    public PageResult<TbSellerVO> findSellerPage(PageBean<TbSeller> pageBean) {
        TbSellerExample example = new TbSellerExample();
        TbSellerExample.Criteria criteria = example.createCriteria();
        if(!StrUtil.isEmptyIfStr(pageBean.getBean().getName())){
            criteria.andNameLike("%"+pageBean.getBean().getName()+"%");
        }
        if(!StrUtil.isEmptyIfStr(pageBean.getBean().getNickName())){
            criteria.andNickNameLike("%"+pageBean.getBean().getNickName()+"%");
        }
        if(!StrUtil.isEmptyIfStr(pageBean.getBean().getStatus())){
            criteria.andStatusEqualTo(pageBean.getBean().getStatus());
        }
        //分页部分
        PageHelper.startPage(pageBean.getPageNumber(),pageBean.getPageSize());
        List<TbSeller> sellers = tbSellerMapper.selectByExample(example);
        PageInfo<TbSeller> info = new PageInfo<>(sellers);
        //转换
        List<TbSellerVO> vos = info.getList().stream().map((x) -> {
            TbSellerVO vo = new TbSellerVO();
            BeanUtils.copyProperties(x, vo);
            return vo;
        }).collect(Collectors.toList());
        return new PageResult<>(info.getPageNum(),info.getPageSize(),info.getTotal(),vos);
    }

    @Override
    public TbSeller selectSellerBySellerId(String sellerId) {
        return tbSellerMapper.selectByPrimaryKey(sellerId);
    }

//审核
    @Override
    public void updateSeller(String status, String sellerId) {
        TbSeller seller = tbSellerMapper.selectByPrimaryKey(sellerId);
        seller.setStatus(status);
        tbSellerMapper.updateByPrimaryKeySelective(seller);
    }

}
